<?php

	namespace controller;

		require_once ("view/HTMLPageView.php");
		require_once ("model/SessionModel.php");
		require_once ("model/LoginModel.php");

		class LoginController
		{
			private $pageView;
			private $returningName;
			private $logInModel;
			private $logOutModel;
			private $user;
			private $session;
			private $content;
			private $message = "";
			private $status = "Inte inloggad";
			private $page;

			public function __construct($returningName)
			{
				$this->returningName = $returningName;
				$this->pageView = new \view\HTMLPageView($this->returningName);
				$this->logInModel = new \model\LoginModel();
				$this->sessionModel = new \model\SessionModel();
			}

			//Det första som händer
			public function startMyApplication()
			{
				$this->user = $this->handleUserInput();
				$this->status = $this->sessionModel->getsessionModelStatus();

				if ($this->sessionModel->isUserLoggedInWithCookies() == true)
				{
					$this->page = $this->pageView->getLoggedInPage($this->sessionModel->getsessionModelMessage(), $this->status, $this->returningName);
				}
				
				else if ($this->logInModel->isUserLoggedIn() == true)
				{
					$this->page = $this->pageView->getLoggedInPage($this->message, $this->status, $this->returningName);
				}
				else
				{
					$this->page = $this->pageView->getPage($this->message, $this->status);
				}

				return $this->page;

			}


			//titta på inskickat data
			//kollar i vyn om någon vill logga in
			public function handleUserInput()
			{
				if ($this->pageView->userWantsToLogOut())
				{
					$this->logInModel->logOutUser();
					$this->sessionModel->logOutFromSession();
					$this->message = $this->sessionModel->getsessionModelMessage();
					$this->status = $this->sessionModel->getsessionModelStatus();
					$this->page = $this->pageView->getPage($this->message, $this->status);
					return;
				}

				if ($this->sessionModel->validateOngoingLogin() == true)
				{
					$this->message = $this->sessionModel->getsessionModelMessage();
					$this->status = $this->sessionModel->getsessionModelStatus();

					//Kollar om sessionen är ok
					if ($this->sessionModel->validateSession() == true)
					{
						$this->message = $this->sessionModel->getsessionModelMessage();
						$this->page = $this->pageView->getLoggedInPage($this->message, $this->status, $this->returningName);
						return;
					}
				}

				if ($this->pageView->userWantsToLogIn())
				{
					try
					{
						$this->user = $this->pageView->getNewUser();
						//Om ny user är skapad
						if ($this->user)
						{
							try
							{

								$validUser = $this->logInModel->validateUser($this->user);
							}
							catch(\Exception $e)
							{
								$this->message = $e->getMessage();
								$this->status = $this->logInModel->getStatus();
								return false;
							}

							if ($validUser == true)
							{
								$validatedUserName = $this->logInModel->getValidatedUserName();
								$validatedUserPassW = $this->logInModel->getRandomPassword();

								$this->sessionModel->setSession($validatedUserName, $validatedUserPassW);
								$this->message = $this->sessionModel->getsessionModelMessage();
								$this->status = $this->sessionModel->getsessionModelStatus();

								if ($this->logInModel->getUsersLogInPreference() == true)
								{
									$this->sessionModel->setCookieInfo($validatedUserName, $validatedUserPassW);
								}

								$this->page = $this->pageView->getLoggedInPage($this->message, $this->status, $this->returningName);
							}
						}

						else//Om det inte går att skapa en användare
						{
							$this->message = $this->logInModel->getMessage();
							$this->status = $this->logInModel->getStatus();
							$this->page = $this->pageView->getPage($this->message, $this->status);
						}
						return;
					}

					catch (\Exception $e)
					{
						throw ($e);
					}

				}

			}


		}
